<?php

/**
* alltosun.com 收藏widget fav.php
* ============================================================================
* 版权所有 (C) 2009-2012 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 钱有明 (qianym@alltosun.com) $
* $Date: 2012-10-8 下午12:30:26 $
* $Id: fav.php 22489 2013-04-08 07:42:35Z weisd $
*/

class fav_widget
{
    private $per_page = 20;

    public function get_list($params = array())
    {
        $page = Request::Get('page_no', 1);
        if (empty($params) || !is_array($params)) {
            return array('info' => 'error', 'msg' => '参数有误');
        }

        $per_page = $params['per_page'] ? $params['per_page'] : $this->per_page;

        $filter = array(
                'user_id'  => user_helper::get_user_id(),
                'res_name' => 'goods',
                'status'   => 1
        );

        $list = array();

        $count = _model('fav')->getTotal($filter);

        if ($count) {
            $pager = new Pager($per_page);
            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }
            $list = _model('fav')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
        }
        return $list;
    }

    /**
     * 添加收藏
     * @param array $params
     * @return mixed
     */
    public function create($params)
    {
        if (empty($params) || !is_array($params)) {
            return array('info' => 'error', 'msg' => '参数有误');
        }

        $info = array(
                    'user_id'     => 0,
                    'res_name'    => '',
                    'res_id'      => 0,
                );

        foreach ($info as $k=>$v) {
            if (!isset($params[$k]) || empty($params[$k])) {
                continue;
            }

            $info[$k] = $params[$k];
            if (!is_numeric($info[$k])) {
                $info[$k] = AnFilter::filter_string($info[$k]);
            }
        }

        if (empty($info['res_name']) || empty($info['res_id'])) {
            return array('info' => 'error', 'msg' => '对不起，请选择要收藏的资源');
        }
        if (empty($info['user_id'])) {
            return array('info' => 'error', 'msg' => '对不起，收藏人不能为空');
        }

        $id = 1;
        if (!_uri('fav', $info)) {
            $id = _model('fav')->create($info);
        }

        return $id;
    }

    /**
     * 显示收藏按扭
     * @param array $params
     * @return array
     */
    public function show_fav_operate($params)
    {
        if (!isset($params['res_name']) && empty($params['res_name'])) {
            return array();
        }

        if (!isset($params['res_id']) && empty($params['res_id'])) {
            return array();
        }

        $fav_user_ids = fav_helper::get_user_ids($params['res_name'], $params['res_id']);

        return $fav_user_ids;
    }

    /**
     * 删除收藏
     * @param array $params
     * @return boolean
     */
    public function delete($params)
    {
        $filter = array();
        if (!isset($params['id']) || empty($params['id'])) {
            return FALSE;
        }
        $filter['id'] = $params['id'];

        return _model('fav')->delete($filter);
    }
}
?>